package com.iss.apartmentmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iss.apartmentmanage.entity.UserInfo;
import com.iss.apartmentmanage.mapper.UserInfoMapper;
import com.iss.apartmentmanage.model.BaseModel;
import com.iss.apartmentmanage.model.UserWithUserInfo;
import com.iss.apartmentmanage.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Arrays;


@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public IPage<UserInfo> selectAllUserInfoMessage(int pageNum, int pageSize, BaseModel baseModel) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(baseModel.getUinfoName())) {
            wrapper.eq("uinfo_name", baseModel.getUinfoName());
        }
        Page<UserInfo> page = new Page<>(pageNum,pageSize);
        IPage<UserInfo> iPage = userInfoMapper.selectPage(page,wrapper);
        return iPage;
    }
    /*
    * String uinfoName,String uinfoGender,
                                  String uinfoIdcard,String uinfoPhone,
                                  String uinfoEmail,String userinfoId*/
    @Override
    public Integer insertUserInfo(@RequestBody UserInfo userInfo) {
        int count=0;
        try {
            if (userInfo != null) {
                count = userInfoMapper.insert(userInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("输入数据异常");
        }
        return count;
    }

    @Override
    public Integer updateUserInfoById(UserInfo userInfo) {
        int count=0;
        try {
            if (userInfo.getId()!=0) {
                 count = userInfoMapper.updateById(userInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("更新数据有误");
        }
        return count;
    }

    @Override
    public Integer deleteUserInfoById(Integer id) {
        int count = 0;
        try{
            if(id!=null){
                count = userInfoMapper.deleteById(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除数据错误");
        }
        return count;
    }
    @Override
    public Integer deleteUserInfoByBatchIds(String[] ids) {
        int count = 0 ;
        try{
            if (ids!=null){
                count = userInfoMapper.deleteBatchIds(Arrays.asList(ids));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public UserWithUserInfo getUserinfoByUsername(String uName){
        UserWithUserInfo userWithUserInfo= null;
        try {
            userWithUserInfo = userInfoMapper.getUserinfoByUsername(uName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return userWithUserInfo;
    }

    @Override
    public Integer updateIsUseByUserinfoId(UserInfo userInfo) {
        int count = 0;
        try{
            count = userInfoMapper.updateById(userInfo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Integer updateIsUsedByUinfoIds(String[] arr, int isUser) {
        int count=0;
        try {
            count =userInfoMapper.updateIsUsedByUinfoIds(arr,isUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public UserWithUserInfo getUserInfoUnameAndUpwdByUid(UserWithUserInfo userinfoId) {
        UserWithUserInfo userWithUserInfo = new UserWithUserInfo();
        if(userinfoId!=null) {
             userWithUserInfo = userInfoMapper.getUserInfoUnameAndUpwdByUid(userinfoId);
        }
        return userWithUserInfo;
    }

    @Override
    public Integer updateUserInfoIdByUser(UserInfo userInfoId) {
        int count=0;
        if(userInfoId!=null) {
            count = userInfoMapper.updateById(userInfoId);
        }
        return count;
    }
}
